<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行','新款', '精选']" @tabClick="tabClick" 
        ref="tabControl1" class="tab-control" v-show="isTabFixed"></tab-control>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="scrollBackTop" 
      :pull-up-load="true" @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperLoad"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control :titles="['流行','新款', '精选']" @tabClick="tabClick" 
        ref="tabControl2"></tab-control>
      <goods-list :goodList="showCurrent"></goods-list>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
  
</template>

<script>
import NavBar from 'components/common/navbar/NavBar'
import Scroll from 'components/common/scroll/Scroll'

import HomeSwiper from './childComps/HomeSwiper'
import RecommendView from './childComps/RecommendView.vue'
import FeatureView from './childComps/FeatureView.vue'

import TabControl from 'components/content/TabControl.vue'
import GoodsList from 'components/content/GoodsList.vue'
import BackTop from 'components/content/BackTop.vue'

import {getHomeMultidata, getHomeGoods} from 'network/home'//乜有用default到处的时候加大括号


export default {
  name: 'Home',
  data(){
    return{
      banners: [],
      recommends: [],
      goods: {
        'pop': {page:0, list: []},
        'new': {page:0, list: []},
        'sell': {page:0, list: []}
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffSetTop: 0,
      isTabFixed: false,
      saveY: 0
    }
  },
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
    BackTop
  },
  created(){
    //1.请求多个数据
    this.getMultidata()
    this.getGoods('pop')
    this.getGoods('new')
    this.getGoods('sell')

    
  },
  mounted(){
    //方法加小括号和不加小括号的区别
    const refresh = this.debounce(this.$refs.scroll.getRefresh, 200)
    this.$bus.$on('imgItemLoad', ()=> {
      // this.$refs.scroll && this.$refs.scroll.getRefresh()
      if(this.$refs.scroll){
        refresh()
      }
    })
  },
  updated(){
    
  },
  activated(){
    this.$refs.scroll.bscroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.getRefresh()
  },
  deactivated(){
    this.saveY = this.$refs.scroll.getSaveY()
  },
  computed: {
    showCurrent(){
      return this.goods[this.currentType].list
    }
  },
  methods: {
    swiperLoad(){
      //获取tabControl的offsetTop
      //每个组件都有一个属性$el，用于获取元素
      this.tabOffSetTop = this.$refs.tabControl2.$el.offsetTop;
    },
    //事件监听相关的方法
    debounce(func, delay){
      let timer = null
      return function(){
        if(timer) clearTimeout(timer)
        timer = setTimeout(()=>{
          func()
        }, delay)
      }
    },
    tabClick(index){
      switch(index){
        case 0 :
          this.currentType = 'pop'
          break;
        case 1 :
          this.currentType = 'new'
          break;
        case 2 :
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    backClick(){
      this.$refs.scroll.bscroll.scrollTo(0,0, 500)
    },
    scrollBackTop(position){
      // console.log(position.y);
      if(-position.y > this.tabOffSetTop){
        this.isTabFixed = true
      } else this.isTabFixed = false
      if(position.y > -1000){
        this.isShowBackTop = false
      } else this.isShowBackTop = true
    },
    loadMore(){
      this.getGoods(this.currentType)
      this.$refs.scroll.bscroll.refresh()
    },
    //网络请求相关的方法
    getMultidata() {
      getHomeMultidata().then(res => { 
      console.log(res);
      this.banners = res.data.banner.list
      this.recommends = res.data.recommend.list
      });
    },
    getGoods(type){
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page++
        this.$refs.scroll.bscroll.finishPullUp()
      })
    },
  }
}
</script>

<style scoped>
  #home {
    /* padding-top: 44px; */
    position: relative;
    height: 100vh;
  }
  .home-nav {
    background: var(--color-tint);
    color: #fff;
    /* 原生滚动时为了不让其和窗口一起滚动 */
    /* position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 999; */
  }
  .tab-control {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }
  .content {
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  /* .tab-fixed {
    position: fixed;
    left: 0;
    top: 44px;
  } */
</style>